package com.vitabuybuy.tv.ui.mine.talent.notice

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.tencent.imsdk.TIMConversationType
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityLiveNoticeDetailBinding
import com.vitabuybuy.tv.ui.mine.talent.notice.adapter.LogisticsAdapter
import com.vitabuybuy.tv.ui.mine.talent.notice.viewmodel.NoticeViewModel
import com.vitabuybuy.tv.ui.mine.talent.task.adapter.TaskGoodAdapter
import com.vitabuybuy.tv.ui.tencent.chat.ChatActivity
import kotlinx.android.synthetic.main.activity_live_notice_detail.*

class LiveNoticeDetailActivity :
    BaseActivity<NoticeViewModel, ActivityLiveNoticeDetailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_live_notice_detail
    private val goodAdapter by lazy { TaskGoodAdapter(false) }
    private val logisticsAdapter by lazy { LogisticsAdapter() }
    private var noticeId: Int? = -1

    override fun onGetBundle(bundle: Bundle?) {
        noticeId = bundle?.getInt("noticeId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.task_detail))
        mBinding.mViewModel = viewModel
        lvGoods.apply {
            adapter = goodAdapter
            layoutManager = LinearLayoutManager(mContext)
            isNestedScrollingEnabled = false
        }
        rlLogistics.apply {
            adapter = logisticsAdapter
            layoutManager = LinearLayoutManager(mContext)
            isNestedScrollingEnabled = false
        }
    }

    override fun observeData() {
        viewModel.getNoticDetails(noticeId)
        viewModel.noticeDetail.observe(this, Observer {
            when (it.status) {
                getString(R.string.wait_to_deliver) -> {
                    rlLogistics.visibility = View.GONE
                    tvNoPassResonHint.visibility = View.GONE
                    tvNoPassReson.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                }
                getString(R.string.wait_to_receive) -> {
                    rlLogistics.visibility = View.VISIBLE
                    tvNoPassResonHint.visibility = View.GONE
                    tvNoPassReson.visibility = View.GONE
                    btnConfirmReceive.visibility = View.VISIBLE
                    btnConfirmReceive.text = getString(R.string.confirm_receive)
                }
                getString(R.string.wait_to_deal) -> {
                    rlLogistics.visibility = View.VISIBLE
                    tvNoPassResonHint.visibility = View.GONE
                    tvNoPassReson.visibility = View.GONE
                    btnConfirmReceive.visibility = View.VISIBLE
                    btnConfirmReceive.text = getString(R.string.apply_auth)
                }
                getString(R.string.wait_auth) -> {
                    rlLogistics.visibility = View.VISIBLE
                    tvNoPassResonHint.visibility = View.GONE
                    tvNoPassReson.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                }
                getString(R.string.no_pass) -> {
                    rlLogistics.visibility = View.VISIBLE
                    tvNoPassResonHint.visibility = View.VISIBLE
                    tvNoPassReson.visibility = View.VISIBLE
                    btnConfirmReceive.visibility = View.VISIBLE
                    tvNoPassReson.text = it.refuseReason
                    btnConfirmReceive.text = getString(R.string.apply_auth)
                }
                getString(R.string.already_pass) -> {
                    rlLogistics.visibility = View.VISIBLE
                    tvNoPassResonHint.visibility = View.GONE
                    tvNoPassReson.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                }
            }
            tvName.text = it.store.name
            tvRequirement.text = it.task.title
            tvRequirementContent.text = String.format(
                getString(R.string.task_requirement),
                it.task.content
            )
            tvTiChengRequirement.text = when (it.commissionType) {
                "提成金额" -> String.format(getString(R.string.ticheng_requirement), it.commission)
                "佣金" -> getString(R.string.youjin_requirement).plus(it.commission) + "%"
                else -> String.format(getString(R.string.yikoujia_requirement), it.commission)
            }
            tvSitRequirement.text = String.format(getString(R.string.pit_fee_1), it.seatPrice)
            tvAreaRequirement.text =
                String.format(getString(R.string.area_requirement), it.label.name)
            tvLiveType.text = String.format(getString(R.string.live_type_1), it.task.liveType)
            tvFansRequirement.text =
                String.format(getString(R.string.fans_requirement), it.task.fansQuantity)
            tvReceiverAddressDetail.text =
                String.format(getString(R.string.deliver_address_2), it.task.address.addressDetail)
            tvLogisticsStatus.text =
                String.format(
                    getString(R.string.deliver_status),
                    if (it.courier.name.isNullOrEmpty()) "暂无物流" else it.courier.name
                )
            goodAdapter.setNewData(it.task.productList)
            for (abc in 0..4) {
                logisticsAdapter.addData(
                    "2020-05-25/周一/04:40:52 商品已经下单\n19:16:26 包裹正在等待揽收"
                )
            }
        })
    }

    override fun eventHandling() {
        btnConfirmReceive.setOnClickListener {
            if (btnConfirmReceive.text.toString() == getString(R.string.confirm_receive)) {
                AlertDialog.Builder(mContext)
                    .setTitle(getString(R.string.logout))
                    .setMessage(getString(R.string.are_you_sure_confir_receiver))
                    .setCancelable(true)
                    .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                        viewModel.comfirmReceiver(
                            mapOf(
                                Pair(
                                    "id",
                                    viewModel.noticeDetail.value?.id
                                )
                            )
                        )
                    }
                    .setNegativeButton(getString(R.string.cancel)) { _, _ -> }.create()
                    .show()
            } else {
                val params = HashMap<String, Any?>()
                params["id"] = viewModel.noticeDetail.value?.id
                viewModel.applyAuth(params)
            }
        }
        btnChatNow.setOnClickListener {
            viewModel.noticeDetail.value?.run {
                val chatInfo = ChatInfo()
                chatInfo.type = TIMConversationType.C2C
                chatInfo.id = this.store.id.toString()
                chatInfo.chatName = this.store.name
                ChatActivity.openActivity(mContext, chatInfo)
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 ,1-> {
                viewModel.getNoticDetails(noticeId)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, noticeId: Int) {
            mContext.startActivity(
                bundleOf(Pair("noticeId", noticeId)),
                LiveNoticeDetailActivity::class.java
            )
        }
    }
}
